/*
 *  Copyright 2008 Mark Ashworth <javameme@gmail.com>.
 * 
 *  Licensed under the GNU General Public License v3 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.gnu.org/licenses/gpl-3.0.html
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package xperiment.metaphor.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.springframework.validation.AbstractErrors;
import org.springframework.validation.DefaultMessageCodesResolver;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.validation.MessageCodesResolver;
import org.springframework.validation.ObjectError;

/**
 * Model errors
 * @author Mark Ashworth <javameme@gmail.com>
 * @version 1.0.0
 */
public class ModelErrors extends AbstractErrors {

    //private Object target;
    
    /** The name of the object that has errors */
    private String objectName;
    
    /** The list of errors associated with the object */
    private List<ObjectError> errors = new ArrayList<ObjectError>();
    
    /** The message code resolver */
    private MessageCodesResolver messageCodesResolver = new DefaultMessageCodesResolver();

    public ModelErrors() {
    }

    public ModelErrors(String objectName) {
        this.objectName = objectName;
    }
    
    /**
     * The message code resolver
     * @return MessageCodesResolver
     */
    public MessageCodesResolver getMessageCodesResolver() {
        return messageCodesResolver;
    }

    /**
     * The message code resolver
     * @param messageCodesResolver The new value
     */
    public void setMessageCodesResolver(MessageCodesResolver messageCodesResolver) {
        this.messageCodesResolver = messageCodesResolver;
    }
    
    /**
     * The name of the object that has errors
     * @return String
     */
    public String getObjectName() {
        return objectName;
    }

    /**
     * The name of the object that has errors
     * @param objectName The new value
     */
    public void setObjectName(String objectName) {
        this.objectName = objectName;
    }
    
    public void addError(ObjectError error) {
		this.errors.add(error);
	}
    
    public void addAllErrors(Errors errors) {
        if (!errors.getObjectName().equals(getObjectName())) {
			throw new IllegalArgumentException("Errors object needs to have same object name");
		}
		this.errors.addAll(errors.getAllErrors());
    }

    public List getGlobalErrors() {
        List result = new LinkedList();
		for (Iterator it = this.errors.iterator(); it.hasNext();) {
			Object error = it.next();
			if (!(error instanceof FieldError)) {
				result.add(error);
			}
		}
		return Collections.unmodifiableList(result);
    }
    
    public List getFieldErrors() {
        List result = new LinkedList();
		for (Iterator it = this.errors.iterator(); it.hasNext();) {
			Object error = it.next();
			if (error instanceof FieldError) {
				result.add(error);
			}
		}
		return Collections.unmodifiableList(result);
    }

    public Object getFieldValue(String field) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
    public void reject(String errorCode, Object[] errorArgs, String defaultMessage) {
        addError(new ObjectError(getObjectName(), resolveMessageCodes(errorCode), errorArgs, defaultMessage));
    }

    public void rejectValue(String field, String errorCode, Object[] errorArgs, String defaultMessage) {
        addError(new FieldError(objectName, field, defaultMessage));
    }
    
    public String[] resolveMessageCodes(String errorCode) {
		return getMessageCodesResolver().resolveMessageCodes(errorCode, getObjectName());
	}
}
